package com.ruoyi.work.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.redis.RedisCache;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.work.domain.TbLocationUser;
import com.ruoyi.work.service.ITbLocationUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 人员定位信息Controller
 *
 * @author szm
 * @date 2025-10-21
 */
@Api(tags = "人员定位信息接口")
@RestController
@RequestMapping("/work/TbLocationUser")
public class TbLocationUserController extends BaseController {
    @Autowired
    private ITbLocationUserService tbLocationUserService;
    /*@Autowired
    private RabbitMQProducerService rabbitmqProducerService;*/
    @Autowired
    private RedisCache redisCache;


    /**
     * 查询人员定位信息列表
     */
    @ApiOperation("查询人员定位信息列表")
    @GetMapping("/list")
    public TableDataInfo list(TbLocationUser entity, HttpServletRequest req) {
        startPage();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.setEntity(entity);
        List<TbLocationUser> list = tbLocationUserService.list(wrapper);
        return getDataTable(list);
    }

    @ApiOperation("根据任务编号和人员编号，查询人员位置信息")
    @GetMapping({"/getByUserWork"})
    public AjaxResult getByUserWork(@RequestParam("workNo") String workNo,
                                    @RequestParam("userNo") String userNo) {
        List<TbLocationUser> tbLongDims = tbLocationUserService.lambdaQuery()
                .eq(TbLocationUser::getWorkNo,workNo)
                .eq(TbLocationUser::getUserNo,userNo)
                .orderByAsc(TbLocationUser::getCreateTime).list();
        return AjaxResult.success("查询成功", tbLongDims);
    }

    @ApiOperation("查询人员位置信息")
    @GetMapping({"/getByUser"})
    public AjaxResult getByUser(TbLocationUser tbLongDim) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.setEntity(tbLongDim);
        List<TbLocationUser> tbLongDims = this.tbLocationUserService.list(wrapper);
        return AjaxResult.success("查询成功", tbLongDims);
    }

    @ApiOperation("保存/修改人员位置信息")
    @GetMapping({"/save"})
    public AjaxResult save(TbLocationUser tbLongDim) {
        tbLongDim.setUpdateTime(new Date());
        tbLongDim.setCreateTime(new Date());
        tbLongDim.setCreateBy(tbLongDim.getUserNo());
        tbLongDim.setUpdateBy(tbLongDim.getUserNo());

        /*String result = rabbitmqProducerService.sendMessage(JSON.toJSONString(tbLongDim), tbLongDim.getUserNo());

        return AjaxResult.success(result);*/
        if (redisCache.hasKey(tbLongDim.getWorkNo())){
            boolean b= tbLocationUserService.insertLocationUser(tbLongDim);
            return AjaxResult.success(b);
        }else{
            return AjaxResult.warn("任务已结束，定位信息停至采集");
        }
    }

    /**
     * 导出人员定位信息列表
     */
    @ApiOperation("导出人员定位信息列表")
    @Log(title = "人员定位信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, TbLocationUser entity) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.setEntity(entity);
        List<TbLocationUser> list = tbLocationUserService.list(wrapper);
        ExcelUtil<TbLocationUser> util = new ExcelUtil<TbLocationUser>(TbLocationUser. class);
        util.exportExcel(response, list, "人员定位信息数据");
    }

    /**
     * 获取人员定位信息详细信息
     */
    @ApiOperation("获取人员定位信息详细信息")
    @GetMapping(value = "/getById")
    public AjaxResult getById(@RequestParam("id") String id) {
        return success(tbLocationUserService.getById(id));
    }

    /**
     * 删除人员定位信息
     */
    @ApiOperation("删除人员定位信息")
    @Log(title = "人员定位信息", businessType = BusinessType.DELETE)
    @PostMapping("/delIds")
    public AjaxResult remove(@RequestParam("ids") String ids) {
        return toAjax(tbLocationUserService.removeById(ids));
    }
}
